package com.example.news.exception;

import entity.Rsp;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {




    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object handleException(Exception e){
        e.printStackTrace();
        if(e instanceof ArithmeticException){
            return Rsp.fail().msg("这是一个算数异常");
        }else if(e instanceof MethodArgumentNotValidException){
            MethodArgumentNotValidException ex= (MethodArgumentNotValidException)e;
            return Rsp.fail().msg(ex.getBindingResult()
            .getFieldError().getDefaultMessage());
        }else if(e instanceof HttpRequestMethodNotSupportedException){
            HttpRequestMethodNotSupportedException ex= (HttpRequestMethodNotSupportedException)e;
            return Rsp.fail().msg(ex.getMessage());
        }else if(e instanceof HttpMessageNotReadableException){
            HttpMessageNotReadableException ex= (HttpMessageNotReadableException)e;
            return Rsp.fail().msg("json数据格式可能发生错误，请检查："+ex.getMessage());
        }else {
            return Rsp.fail().msg("这是一个未知异常");
        }



    }



//    @ExceptionHandler(value = AuthenticationException.class)
//    @ResponseBody
//    public Object handleException(AuthenticationException e){
//        return MyRsp.error().msg(e.getMessage());
//    }

}
